package com.ue.testffmpegexecutor;

import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.crust87.ffmpegexecutor.FFmpegExecutor;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

public class MainActivity extends AppCompatActivity {
    private static final String TAG=MainActivity.class.getSimpleName();
    private TextView tv_tip;
    private FFmpegExecutor mExecutor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tv_tip = (TextView) findViewById(R.id.tv_tip);
        tv_tip.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                execCmd();
            }
        });
        initFFmpeg();
    }

    private void initFFmpeg() {
        try {
            InputStream ffmpegFileStream = getApplicationContext().getAssets().open("ffmpeg");
            mExecutor = new FFmpegExecutor(getApplicationContext(), ffmpegFileStream);
            mExecutor.setFFmepgExecutorListener(new FFmpegExecutor.FFmepgExecutorListener() {
                @Override
                public void onReadProcessLine(String line) {
                    logE("process line=" + line);
                }

                @Override
                public void onFinishProcess() {
                    logE("finish process");
                }

                @Override
                public void onError(Exception e) {
                    logE("error:" + e.getMessage());
                }
            });
            mExecutor.init();
        } catch (IOException e) {
            // TODO something
        } catch (InterruptedException e) {
            // TODO something
        }
    }

    private void cutVideo(){
        if (mExecutor == null) {
            return;
        }
        File musicDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC);
        //截取视频，起始时间-00:00:03，截取的时间段-00:00:08----播放的时候快进/后退会视频卡死
//        ffmpeg -i input.mp4 -ss 00:00:03 -t 00:00:08 -async 1 -strict -2 output.mp4

//        截取视频，不重编码，播放正常
//        ffmpeg -i in.mp4 -ss [start] -t [duration] -c copy out.mp4
        mExecutor.putCommand("-y")
                .putCommand("-i")
                .putCommand(musicDir.getAbsolutePath() + "/tttt.mp4")
                .putCommand("-ss")
                .putCommand("00:00:00")
                .putCommand("-t")
                .putCommand("00:00:30")
                .putCommand("-c")
                .putCommand("copy")
                .putCommand(musicDir.getAbsolutePath() + "/tttt_cut.mp4");

        mExecutor.executeCommandAsync();
    }

    private void speedupVideo(){
        if (mExecutor == null) {
            return;
        }
        File musicDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC);

        mExecutor.putCommand("-y")
                .putCommand("-i")
                .putCommand(musicDir.getAbsolutePath() + "/tttt.mp4")
                .putCommand("-filter:v")
                .putCommand("setpts=0.5*PTS")
                .putCommand("-an")
                .putCommand(musicDir.getAbsolutePath() + "/tttt_accelerate.mp4");

        mExecutor.executeCommandAsync();
    }

    private void execCmd() {
        if (mExecutor == null) {
            return;
        }
        File musicDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC);

        mExecutor.putCommand("-y")
                .putCommand("-i")
                .putCommand(musicDir.getAbsolutePath() + "/tttt.mp4")
                .putCommand("-filter:v")
                .putCommand("setpts=0.5*PTS")
                .putCommand("-an")
                .putCommand(musicDir.getAbsolutePath() + "/tttt_accelerate.mp4");

//        String[] cmds = new String[]{
//                libffmpeg.getAbsolutePath(),
//                "-i",
//                musicDir.getAbsolutePath() + "/tttt.mp4",
//                "-filter:v",
//                speedCmd,
//                "-an",
//                musicDir.getAbsolutePath() + "/tttt_accelerate.mp4",
//        };

//        mExecutor.putCommand("-y")
//                .putCommand("-i")
//                .putCommand(originalPath)
//                .putCommand("-vcodec")
//                .putCommand("libx264")
//                .putCommand("-profile:v")
//                .putCommand("baseline")
//                .putCommand("-level")
//                .putCommand("3.1")
//                .putCommand("-b:v")
//                .putCommand("1000k")
//                .putCommand("-vf")
//                .putCommand(filter)
//                .putCommand("-c:a")
//                .putCommand("copy")
//                .putCommand(Environment.getExternalStorageDirectory().getAbsolutePath() + "/result.mp4");

        mExecutor.executeCommandAsync();
    }

    private void logE(String msg){
        Log.e(TAG,msg);
    }
}
